package cn.stylefeng.guns.modular.basic.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.BasicArea;
import cn.stylefeng.guns.modular.basic.entity.BasicWarehouse;
import cn.stylefeng.guns.modular.basic.model.params.BasicAreaParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicAreaResult;
import cn.stylefeng.guns.modular.basic.service.BasicAreaService;
import cn.stylefeng.guns.modular.basic.service.BasicWarehouseService;
import cn.stylefeng.guns.sys.core.util.CacheUtil;
import cn.stylefeng.guns.sys.modular.system.entity.Dept;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.DeptService;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;

/**
 * 库区表控制器
 */
@Controller
@RequestMapping("/basicArea")
public class BasicAreaController extends BaseController {

    private String PREFIX = "/basic/basicArea";

    @Autowired
    private BasicAreaService basicAreaService;

    @Autowired
    private UserService userService;

    @Autowired
    private BasicWarehouseService basicWarehouseService;

    /**
     * 跳转到主页面
     */
    @RequestMapping("")
    public String index(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	
    	//查询可选择仓库
    	model.addAttribute("wlist", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/basicArea.html";
    }
    
    /**
     * 跳转到主页面
     */
    @RequestMapping("index_hx")
    public String index_hx(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	
    	//查询可选择仓库
    	model.addAttribute("wlist", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/basicArea_hx.html";
    }

    /**
     * 新增页面
     *
     * @author zhucx
     * @Date 2019-07-01
     */
    @RequestMapping("/add")
    public String add(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	
    	//查询可选择仓库
    	model.addAttribute("wlist", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/basicArea_add.html";
    }

    /**
     * 新增页面
     *
     * @author zhucx
     * @Date 2019-07-01
     */
    @RequestMapping("/add_hx")
    public String add_hx(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	
    	//查询可选择仓库
    	model.addAttribute("wlist", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/basicArea_add_hx.html";
    }

    /**
     * 批量分配物料大类页面
     * @author zhucx
     * @Date 2019-09-17
     */
    @RequestMapping("/toAssignMaterial")
    public String toAssignMaterial(Long[] ids) {
        return PREFIX + "/assignMaterial.html";
    }

    /**
     * 编辑页面
     *
     * @author zhucx
     * @Date 2019-07-01
     */
    @RequestMapping("/edit")
    public String edit(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	
    	//查询可选择仓库
    	model.addAttribute("wlist", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/basicArea_edit.html";
    }

    /**
     * 编辑页面
     *
     * @author zhucx
     * @Date 2019-07-01
     */
    @RequestMapping("/edit_hx")
    public String edit_hx(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	
    	//查询可选择仓库
    	model.addAttribute("wlist", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/basicArea_edit_hx.html";
    }

    /**
     * 新增接口
     *
     * @author zhucx
     * @Date 2019-07-01
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(BasicAreaParam basicAreaParam) {
    	Object areaWarehouseId = basicAreaParam.getAreaWarehouseId();
    	if (areaWarehouseId!=null) {
			BasicWarehouse bawBasicWarehouse = basicWarehouseService.getById(Integer.parseInt(areaWarehouseId.toString()));
			basicAreaParam.setAreaWarehouseName(bawBasicWarehouse.getWarehouseName());
		}
        
        //set 交付id
    	Long DeliverId = userService.getDeliverId();
    	if (DeliverId!=null) {
    		basicAreaParam.setDeliverId(DeliverId);
		}
        this.basicAreaService.add(basicAreaParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author zhucx
     * @Date 2019-07-01
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(BasicAreaParam basicAreaParam) {
    	Object areaWarehouseId = basicAreaParam.getAreaWarehouseId();
    	if (areaWarehouseId!=null) {
			BasicWarehouse bawBasicWarehouse = basicWarehouseService.getById(Integer.parseInt(areaWarehouseId.toString()));
			basicAreaParam.setAreaWarehouseName(bawBasicWarehouse.getWarehouseName());
		}
        this.basicAreaService.update(basicAreaParam);
        return ResponseData.success();
    }

    /**
     * 分配物料大类编码
     * @author zhucx
     * @Date 2019-09-17
     */
    @RequestMapping("/assignMaterial")
    @ResponseBody
	@Transactional(rollbackFor = Exception.class)
    public ResponseData assignMaterial(String bigtype,Long[] ids) {
    	for (Long id : ids) {
    		BasicAreaParam param = new BasicAreaParam();
    		BasicArea basicArea = this.basicAreaService.getById(id);
			if (basicArea.getBigtypes()!=null) {
				if (basicArea.getBigtypes().indexOf(bigtype)==-1) {
					basicArea.setBigtypes(basicArea.getBigtypes()+bigtype+";");
	    			ToolUtil.copyProperties(basicArea,param);
	    			this.basicAreaService.update(param);
				} 
			} else {
				basicArea.setBigtypes(bigtype+";");
    			ToolUtil.copyProperties(basicArea,param);
    			this.basicAreaService.update(param);
			}
		}
        return ResponseData.success();
    }

    /**
     * 批量分配物料大类编码
     * @author zhucx
     * @Date 2019-09-17
     */
    @RequestMapping("/btnassignMaterial")
    @ResponseBody
	@Transactional(rollbackFor = Exception.class)
    public ResponseData btnassignMaterial(String[] bigtypes,Long[] ids) {
    	for (Long id : ids) {
    		for (String bigtype : bigtypes) {
        		BasicAreaParam param = new BasicAreaParam();
        		BasicArea basicArea = this.basicAreaService.getById(id);
    			if (basicArea.getBigtypes()!=null) {
    				if (basicArea.getBigtypes().indexOf(bigtype)==-1) {
    					basicArea.setBigtypes(basicArea.getBigtypes()+bigtype+";");
    	    			ToolUtil.copyProperties(basicArea,param);
    	    			this.basicAreaService.update(param);
    				} 
    			} else {
    				basicArea.setBigtypes(bigtype+";");
        			ToolUtil.copyProperties(basicArea,param);
        			this.basicAreaService.update(param);
    			}
			}
		}
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author zhucx
     * @Date 2019-07-01
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(BasicAreaParam basicAreaParam) {
        this.basicAreaService.delete(basicAreaParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author zhucx
     * @Date 2019-07-01
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(BasicAreaParam basicAreaParam) {
        BasicArea detail = this.basicAreaService.getById(basicAreaParam.getId());
        return ResponseData.success(detail);
    }
    
    /**
     * 通过仓库id
     * 查询库区
     */
    @ResponseBody
    @RequestMapping("/queryArea")
    public List<BasicAreaResult> queryArea(BasicAreaParam basicAreaParam){
    	return this.basicAreaService.findListBySpec(basicAreaParam, null,null);
    }

    /**
     * 查询列表
     *
     * @author zhucx
     * @Date 2019-07-01
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(BasicAreaParam basicAreaParam,String condition ) {
    	User user = userService.getcurrentUser();
    	basicAreaParam.setDeliverId(user.getDeliverId());
    	Long deptId = user.getDeptId();
    	
        return this.basicAreaService.findPageBySpec(basicAreaParam,condition,deptId,user.getOperateId());
    }

    /**
     * 查询列表
     * @author zhucx
     * @Date 2019-07-01
     */
    @ResponseBody
    @RequestMapping("/Querylist")
    public List<BasicAreaResult> Querylist(BasicAreaParam basicAreaParam ) {
    	User user = userService.getcurrentUser();
    	basicAreaParam.setDeliverId(user.getDeliverId());
    	Long deptId = user.getDeptId();
    	return this.basicAreaService.findListBySpec(basicAreaParam, deptId,user.getOperateId());
    }

}


